/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragSource;
import java.util.prefs.Preferences;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import org.isqlviewer.model.ThrowableOutlineModel;
import org.isqlviewer.swing.WizardPanel;
import org.isqlviewer.swing.action.SwingEventManager;
import org.isqlviewer.swing.outline.JOutline;
import org.isqlviewer.ui.laf.EnhancedTableCellRenderer;
import org.isqlviewer.ui.listeners.ThrowableTreeDragListener;
import org.isqlviewer.ui.renderer.ThrowableTreeCellRenderer;

/**
 * Basic view for displaying Java throwable objects.
 * <p>
 * 
 * @author Mark A. Kobold &lt;mkobold at isqlviewer dot com&gt;
 * @version 1.0
 */
public class ThrowableView extends AbstractApplicationView {

    private Throwable error = null;
    private String message = null;
    private JScrollPane scrollPanel = null;

    public ThrowableView(Throwable error, String message) {

        this.error = error;
        this.message = message;
    }

    public void configureMenubar(JMenuBar menuBar) {

    }

    public void disposeView(Preferences preferences) {

    }

    public void doLayout(JComponent parentComponent, Preferences preferences, SwingEventManager eventManager) {

        parentComponent.setLayout(new WizardPanel.Layout());
        JPanel panel = new JPanel(new GridBagLayout());

        Object constraint = null;
        constraint = constrain(0, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL);
        panel.add(new JLabel(message), constraint);
        if (error != null) {

            JOutline outline = new JOutline(new ThrowableOutlineModel(error));
            outline.setRootVisible(false);
            outline.setTreeCellRenderer(new ThrowableTreeCellRenderer(outline));

            ThrowableTreeDragListener dragListener = new ThrowableTreeDragListener(outline);
            DragSource dndDragSource = DragSource.getDefaultDragSource();
            dndDragSource.createDefaultDragGestureRecognizer(outline, DnDConstants.ACTION_COPY, dragListener);

            TableColumnModel columnModel = outline.getColumnModel();
            TableColumn column = columnModel.getColumn(1);
            column.setCellRenderer(new EnhancedTableCellRenderer());

            scrollPanel = new JScrollPane(outline);
            scrollPanel.setPreferredSize(new Dimension(320, 200));
            scrollPanel.setMinimumSize(scrollPanel.getPreferredSize());
            scrollPanel.setMaximumSize(scrollPanel.getPreferredSize());
            constraint = constrain(0, 1, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
            panel.add(scrollPanel, constraint);
        }
        parentComponent.add(panel);
        parentComponent.invalidate();
    }

    public void initializeView() {

    }
}
